package com.hly.musicbackground.model.music.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hly.musicbackground.dao.LyricDao;
import com.hly.musicbackground.domain.Lyric;
import com.hly.musicbackground.model.music.VO.LyrVO;
import com.hly.musicbackground.model.music.service.LyricService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class LyricServiceImpl extends ServiceImpl<LyricDao, Lyric> implements LyricService {

    private final MusicKUWOServiceImpl musicKUWOService;

    @Override
    public List<LyrVO> getLyric(int rid) {

        //查询数据库
        Lyric lyric = lambdaQuery().eq(Lyric::getRid, rid).one();

        //若数据库没有歌词则从网络中爬取歌词，并保存到数据库
        if (lyric == null){
            lyric = new Lyric();
            lyric.setRid(rid);
            lyric.setLrc(musicKUWOService.getLrc(String.valueOf(rid)));
            save(lyric);
        }

        //将json转换为实体类
        List<LyrVO> lyrVO = JSON.parseArray(lyric.getLrc(), LyrVO.class);
        return lyrVO;
    }
}
